package cn.com.game.link.entity;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;

import cn.com.game.link.component.LogicComponent;
import cn.com.game.link.component.RenderComponent;
import cn.com.game.link.utils.TextUtil;

public class LinkItem implements RenderComponent, LogicComponent {

	public static final int LINK_ITEM_WIDTH = 50;
	public static final int LINK_ITEM_HEIGHT = 50;
	
	private Rectangle hitbox;
	private int type;
	private IconRender iconRender;
	private boolean selected = false;
	private boolean cleaned = false;
	private Point location;
	private boolean ready;
	
	
	
	 public interface IconRender {
		void render(Graphics g, LinkItem item);
	}
	
	
	public boolean isReady() {
		return ready;
	}


	public void setReady(boolean ready) {
		this.ready = ready;
	}


	@Override
	public String toString() {
		return "LinkItem [hitbox=" + hitbox + ", type=" + type + ", iconRender=" + iconRender + ", selected="
				+ selected + ", cleaned=" + cleaned + ", location=" + location + "]";
	}


	public Point getLocation() {
		return location;
	}


	public void setLocation(Point location) {
		this.location = location;
	}


	public LinkItem(int x, int y, int type) {
		this(new Rectangle(x, y, LINK_ITEM_WIDTH, LINK_ITEM_HEIGHT), type);
	}
	
	
	public LinkItem(int x, int y, int width,int height, int type) {
		this(new Rectangle(x, y, width, height), type);
	}
	
	
	public LinkItem(Rectangle hitbox, int type) {
		this.hitbox = hitbox;
		this.type = type;
	}

	@Override
	public void update(double deltaU) {
	}

	public int getX() {
		return location.x;
	}
	

	public int getY() {
		return location.y;
	}
	
	@Override
	public void render(Graphics g) {
		if(this.cleaned) return;
		if (null != iconRender) {
			iconRender.render(g, this);
		} else {
			g.setColor(Color.WHITE);
			g.fillRect(
					hitbox.x, 
					hitbox.y, 
					hitbox.width, 
					hitbox.height);
			g.setColor(Color.BLACK);
			TextUtil.center(this.hitbox, String.valueOf(type)).accept(g);
		}
		
		if (this.selected) {
			g.setColor(new Color(0, 0, 0xca, 80));
			g.fillRect(hitbox.x, 
				hitbox.y, 
				hitbox.width, 
				hitbox.height);
			g.setColor(new Color(0, 0, 0xca));
			Stroke stroke = new BasicStroke(2,
					BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
			((Graphics2D)g).setStroke(stroke);
			g.drawRect(hitbox.x, 
					hitbox.y, 
					hitbox.width, 
					hitbox.height);
		}
		
		
	}
	


	public IconRender getIconRender() {
		return iconRender;
	}


	public void setIconRender(IconRender iconRender) {
		this.iconRender = iconRender;
	}


	public Rectangle getHitbox() {
		return hitbox;
	}


	public void setHitbox(Rectangle hitbox) {
		this.hitbox = hitbox;
	}


	public int getType() {
		return type;
	}


	public void setType(int type) {
		this.type = type;
	}


	public boolean click(int x, int y) {
		if (hitbox.contains(x, y)) {
			this.selected = !this.selected;
			return true;
		}
		return false;
	}


	public boolean isSelected() {
		return selected;
	}


	public void setSelected(boolean selected) {
		this.selected = selected;
	}


	public boolean isCleaned() {
		return cleaned;
	}


	public void setCleaned(boolean cleaned) {
		this.cleaned = cleaned;
	}
	
	
	
	

}
